/**
 * 继承了 jquery.validate 又提供了很多方法，
 * isAdd 可扩展
 */
(function($){
	function trimLeft(str){
		if(str!=null){
			if(str.charAt(0)=='/'){
				return slice(1,str.length);
			}
		}
		return str;
	}
	function trimRight(str){
		if(str!=null){
			if(str.charAt(str.length-1)=='/'){
				return slice(0,str.length-1);
			}
		}
		return str;
	}
	

	$.fn.form = function(cmd,arg0,arg1,arg2){
		
		var __opt__ = {
				//标记小红点
				requirePoint : function(form){
					$(form).find(".form-group:has([required]) label").prepend('<font style="color:red">*</font>');
				},
				isAdd : function(form,arg0,arg1,arg2){
					var id = $(form).find("[name=id]").val();
					if(id==""||id==null||id==undefined){
						return true;
					}else{
						return false;
					}
				},
				getMsg:function(form,arg0,arg1,arg2){
					var str = arg0;
					if($(form).form("isAdd")){
						return "保存"+((str==null||str==undefined)?"":str);
					}else{
						return "更新"+((str==null||str==undefined)?"":str);
					}
				},
				getUrl :function(form,arg0,arg1,arg2){
					var url = trimRight(arg0);
					var save = arg1;
					if(save==null){
						save="save";
					}else{
						save = trimLeft(save);
					}
					var update = arg2;
					if(update==null){
						update="update";
					}else{
						update = trimLeft(update);
					}
					if($(form).form("isAdd")){
						return url+"/"+save;
					}else{
						return url+"/"+update;
					}
				},
				getSerializeArray:function(form,arg0,arg1,arg2){
					var paramArr = $(form).serializeArray();
					$(form).find(":checkbox").each(function(){
						if($(form).val()=="true" && $(form).prop("checked")==false && $(form).attr("name")!=undefined){
							paramArr.push({"name":$(form).attr("name"),"value":"false"});
						}
					});
					return paramArr;
				},
				getSerialize:function(form,arg0,arg1,arg2){
					var arr = $(form).form("getSerializeArray");
					return $.param(arr);
				},
				
				//jquery.validate 部分
				onfocusout: function(element) { $(element).valid(); },
				errorClass:"invalid",
				errorPlacement:function(error,element) {
					$(element).closest(".form-group").addClass("has-error");
					$(element).tooltip({
						title:error.text()
					}).tooltip("show");
				},
				unhighlight: function(element, errorClass) {  
				     $(element).closest(".form-group").removeClass("has-error");
				     $(element).tooltip("destroy");  
				}
		};
		
		var $ajaxLoadingBtn=null;
		var ajaxLoadingUrl=null;
		
		if(typeof cmd =="string"){
			if(cmd=="isAdd"){
				return __opt__.isAdd(this);
			}else if(cmd=="getMsg"){
				return __opt__.getMsg(this,arg0,arg1,arg2);
			}else if(cmd=="getUrl"){
				return __opt__.getUrl(this,arg0,arg1,arg2);
			}else if(cmd=="getSerializeArray"){
				return __opt__.getSerializeArray(this,arg0,arg1,arg2);
			}else if(cmd=="getSerialize"){
				return __opt__.getSerialize(this,arg0,arg1,arg2);
			}
		}else if(cmd==null || $.isPlainObject(cmd)|| $.isFunction(cmd)){
			var $form = $(this);
			if($(this).attr("__form_init__")==null){
				$(this).attr("__form_init__",true);
				if($.isFunction(cmd)){ //function(form)
					$.extend(__opt__,{
						"submitHandler" : cmd
					});
				}else if($.isPlainObject(cmd)){
					$.extend(__opt__,cmd);
				}
				
				
				__opt__.requirePoint && __opt__.requirePoint(this);
				
				$(document).ajaxSend(function(event, jqxhr, settings, thrownError) {
					if($(event.currentTarget.activeElement).closest("form").get(0) == $form.get(0)){
						$ajaxLoadingBtn=$(event.currentTarget.activeElement);
						ajaxLoadingUrl=event.target.URL;
						$ajaxLoadingBtn.button("loading");
					}
				}).ajaxComplete(function(event, jqxhr, settings, thrownError) {
					if(ajaxLoadingUrl==event.target.URL){
						$ajaxLoadingBtn.button("reset");
					};
				});
//				.ajaxError(function(event, jqxhr, settings, thrownError) {
//					if(ajaxLoadingUrl==event.target.URL){
//						$.messagebox.alert(settings.url+":"+thrownError,"danger");
//					};
//				});
				
				return this.validate(__opt__);
			}
		} 
		
	}
	
})(jQuery);
